<?php

require_once 'adodb5/adodb-errorhandler.inc.php';
require_once 'adodb5/adodb.inc.php';

abstract class Sync_Mongo_Mysql {

    private $_conn_adodb, $_conn_mongo, $_db_mongo, $_count = 1;

    /**
     * 
     * @param type $dbtype
     * @param type $host
     * @param type $username
     * @param type $pass
     * @param type $dbname
     * 
     */
    public function __construct($host = "", $username = "", $pass = "", $dbname = "", $dbtype = "mysql") {
        try {
            $this->_conn_adodb = &ADONewConnection($dbtype);
            $this->_conn_adodb->Connect($host, $username, $pass, $dbname);
            $this->_conn_adodb->execute("set names utf8");
            $this->_conn_mongo = new MongoClient();
            $this->_db_mongo = $this->_conn_mongo->selectDB($dbname);
        } catch (Exception $exc) {
            die("Connection MongoDB Fail...");
        }
    }

//    function __destruct() {
//        $this->_conn_adodb->close();
//        $this->_conn_mongo->close();
//    }

    /**
     * 
     * @param array $values
     * @param type $table
     * 
     */
    public function insert($table, array $values) {
        $query = "INSERT INTO $table SET ";
        $count = count($values);
        foreach ($values as $key => $value) {
            if ($this->_count == $count)
                $query .= "$key='{$value}' ";
            else
                $query .= "$key='{$value}', ";
            $this->_count++;
        }
        try {
            $this->_count = 1;
            $this->_conn_adodb->execute($query);
            $id = $this->_conn_adodb->Insert_Id();
            $values = $values + array("_id" => $id);
			foreach ($values as $key => $value) {
				if( is_numeric($value) )
				{
					$values[$key] = intval($value);
				}
				else if( is_float($value) )
				{
					$values[$key] = (float)$value;
				}
			}
            $this->_db_mongo->selectCollection($table)->save($values);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die();
        }
    }

    /**
     * 
     * @param type $table
     * @param array $values
     * @param type $id
     * 
     */
    public function update($table, array $values, $id) {
        $query = "UPDATE $table SET ";
        $count = count($values);
        foreach ($values as $key => $value) {
            if ($this->_count == $count)
                $query .= "$key='{$value}' ";
            else
                $query .= "$key='{$value}', ";
            $this->_count++;
        }
        $query .= "WHERE id='{$id}'";
        
        try {
            $this->_conn_adodb->execute($query);
            $values = $values + array("_id" => $id);
			foreach ($values as $key => $value) {
				if( is_numeric($value) )
				{
					$values[$key] = intval($value);
				}
				else if( is_float($value) )
				{
					$values[$key] = (float)$value;
				}
			}
            $this->_db_mongo->selectCollection($table)->save($values);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die();
        }
    }

    /**
     * 
     * @param type $table
     * @param type $option_id
     * 
     */
    public function delete($table, $option_id) {
        $id = array("_id" => $option_id);
        $query = "DELETE FROM $table FROM id='{$option_id}'";
        try {
            $this->_conn_adodb->execute($query);
            $this->_db_mongo->selectCollection($table)->remove($id);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die();
        }
    }
    /**
     * 
     * @param type $table
     * @param array $query
     * @param array $fields
     * @return MongoCursor
     * 
     */
    public function find($table, array $query = array(), array $fields = array()) {
        return $this->_db_mongo->selectCollection($table)->find($query, $fields);
    }
    /**
     * 
     * @param type $table
     * @param array $key
     * @param array $initial
     * @param array $reduce
     * @return MongoCursor
     * 
     */
    public function group($table, array $key = array(), array $initial = array(), array $reduce = array(), array $condition = array()){
        return $this->_db_mongo->selectCollection($table)->group($key, $initial, $reduce, $condition);
    }
}

class Mongo_Mysql extends Sync_Mongo_Mysql{
                
    public function __construct() {
        parent::__construct(host, username, password, database, type);
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
